package com.example.wto.myapplication;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ParseJsonUtil
{
	public static Teacher ParseFromJson(String json)
	{
			Teacher teacher=new Teacher();
		try
		{
			JSONObject jsonObject=new JSONObject(json);
			String id=jsonObject.getString("id");
			String name=jsonObject.getString("name");
			teacher.setId(id);
			teacher.setName(name);
			JSONArray coursesJson=jsonObject.getJSONArray("courses");
			List<DayOfWeek> courses=teacher.getWeekList();
			courses.clear();
			JSONObject weekDayJson=null;
			JSONArray  courseJson =null;
			DayOfWeek weekDay=null;
			//��ȡһ��7��Ŀα�
			for(int i=0;i<coursesJson.length();i++)
			{
				weekDayJson=coursesJson.getJSONObject(i);
				//��ȡ���ڼ�������
				weekDay=new DayOfWeek(weekDayJson.getString("weekName"));
				weekDay.getCourseList().clear();
				courseJson=weekDayJson.getJSONArray("course");
				//��ȡ���ڼ��ĵ�һ�ڿΣ��ڶ��ڿΡ�������
				for(int j=0;j<courseJson.length();j++)
				{
					weekDay.getCourseList().add(new Course(courseJson.getString(j)));
				}
				courses.add(weekDay);
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}

		return teacher;
	}
}
